package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Picture;
import com.itheima.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图片表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/picture")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class PictureController {
    @Autowired
    private PictureService pictureService;

    /**
     * 增加图片集合
     * @param pictureList
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody List<Picture>pictureList){
        for (Picture picture : pictureList) {
            pictureService.add(picture);
        }
        return new Result("success","操作成功");
    }

    /**
     * 删除图片
     * @param picture
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Picture picture){
        pictureService.delete(picture);
        return new Result("success","操作成功");
    }
    /**
     * 修改图片数据
     * @param picture
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Picture picture){
        pictureService.edit(picture);
        return new Result("success","操作成功");
    }

    /**
     * 分页查询
     * @param searchMap
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String,Object> searchMap){
        Page<Picture> pageList =pictureService.findPage(searchMap);
        com.itheima.entity.Page page = new com.itheima.entity.Page();

        page.setCurrent((Integer) searchMap.get("currentPage"));
        page.setRecords(pageList.getResult());
        page.setTotal((int) pageList.getTotal());

        return new Result("success", page);
    }

    @PostMapping("/setCover")
    public Result setCover(@RequestBody Picture picture){
        try {
            pictureService.setCover(picture);
        } catch (Exception e) {
            return new Result("error","操作失败");
        }
        return new Result("success","操作成功");
    }
}

